package com.jeesite.modules.task.dceconomicindicator.web;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.common.service.SysOfficeUnitService;
import com.jeesite.modules.sys.entity.Office;
import com.jeesite.modules.task.dceconomicindicator.entity.DcSupTaskScore;
import com.jeesite.modules.task.dceconomicindicator.service.DcSupTaskScoreService;
import com.jeesite.modules.utils.OfficeUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Size;
import java.util.List;
import java.util.Map;

/**
 * 任务扣分表Controller
 * @author xhc
 * @version 2022-10-29
 */
@Controller
@RequestMapping(value = "${adminPath}/dceconomicindicator/dcSupTaskScore")
public class DcSupTaskScoreController extends BaseController {

	@Autowired
	private DcSupTaskScoreService dcSupTaskScoreService;
	@Autowired
	private SysOfficeUnitService sysOfficeUnitService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public DcSupTaskScore get(String scoreId, boolean isNewRecord) {
		return dcSupTaskScoreService.get(scoreId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("dceconomicindicator:dcSupTaskScore:view")
	@RequestMapping(value = {"list", ""})
	public String list(DcSupTaskScore dcSupTaskScore, Model model) {
		if(StringUtils.equals(dcSupTaskScore.getEchart(),"1")){
			String point_office = Global.getConfig("sys.score.pointOffice");
			Office latestUnitByOffice = sysOfficeUnitService.getLatestUnitByOffice();
			if(latestUnitByOffice.getOfficeCode()!=null){
				if(point_office.contains(latestUnitByOffice.getOfficeCode())){
					dcSupTaskScore.setCreateUnit(latestUnitByOffice.getOfficeCode());
				}
			}else{
				dcSupTaskScore.setCreateUnit(null);
			}
		}
		model.addAttribute("dcSupTaskScore", dcSupTaskScore);
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		List<Map> list =dcSupTaskScoreService.queryCreateUnit();
		model.addAttribute("createUnitList", list);
		model.addAttribute("currentYear",currentYear);
		return "modules/task/dceconomicindicator/dcSupTaskScoreList";
	}
	/**
	 * 查询列表
	 */
	@RequiresPermissions("dceconomicindicator:dcSupTaskScore:view")
	@RequestMapping(value = "listDes")
	public String listDes(DcSupTaskScore dcSupTaskScore, Model model) {
		if(StringUtils.equals(dcSupTaskScore.getEchart(),"1")){
			String point_office = Global.getConfig("sys.score.pointOffice");
			Office latestUnitByOffice = sysOfficeUnitService.getLatestUnitByOffice();
			if(latestUnitByOffice.getOfficeCode()!=null){
				if(point_office.contains(latestUnitByOffice.getOfficeCode())){
					dcSupTaskScore.setCreateUnit(latestUnitByOffice.getOfficeCode());
				}
			}else{
				dcSupTaskScore.setCreateUnit(null);
			}
		}
		model.addAttribute("dcSupTaskScore", dcSupTaskScore);
		return "modules/task/dceconomicindicator/dcSupTaskScoreListDes";
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("dceconomicindicator:dcSupTaskScore:view")
	@RequestMapping(value = "index")
	public String index(DcSupTaskScore dcSupTaskScore, Model model) {
		model.addAttribute("dcSupTaskScore", dcSupTaskScore);
		return "modules/task/dceconomicindicator/dcSupTaskScoreIndex";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("dceconomicindicator:dcSupTaskScore:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<DcSupTaskScore> listData(DcSupTaskScore dcSupTaskScore, HttpServletRequest request, HttpServletResponse response) {
		dcSupTaskScore.setPage(new Page<>(request, response));
		if(StringUtils.equals(dcSupTaskScore.getEchart(),"1")){
			String point_office = Global.getConfig("sys.score.pointOffice");
			Office latestUnitByOffice = sysOfficeUnitService.getLatestUnitByOffice();
			if(latestUnitByOffice.getOfficeCode()!=null){
				if(point_office.contains(latestUnitByOffice.getOfficeCode())){
					dcSupTaskScore.setCreateUnit(latestUnitByOffice.getOfficeCode());
					dcSupTaskScore.setTaskYear( Integer.parseInt(DateUtils.getYear())+"");
				}
			}
		}
		Page<DcSupTaskScore> page = dcSupTaskScoreService.findPage(dcSupTaskScore);
		page.getList().forEach(
				x->{
					 String scoreReason = x.getScoreReason();
					String[] split = scoreReason.split(",");
					int yuqi = 0;
					int fanku = 0;
					int wwc = 0;
					int zhsx = 0;
					for(String s:split){
						if(StringUtils.equals("逾期未反馈",s)){
							yuqi++;
						}
						if(StringUtils.equals("逾期反馈",s)){
							fanku++;
						}
						if(StringUtils.equals("未完成",s)){
							wwc++;
						}
						if(StringUtils.equals("滞后时序",s)){
							zhsx++;
						}
					}
					String reason="";
					if(yuqi >0){
						reason = yuqi+"次逾期未反馈 ;";
					}
					if(fanku >0){
						reason = reason+fanku+"次逾期反馈 ;";
					}
					if(wwc >0){
						reason = reason+wwc+"次未完成 ;";
					}
					if(zhsx >0){
						reason = reason+zhsx+"次滞后时序 ;";
					}
					x.setScoreReason(reason);
				}
		);
		return page;
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("dceconomicindicator:dcSupTaskScore:view")
	@RequestMapping(value = "listDataDes")
	@ResponseBody
	public Page<Map<String, Object>> listDataDes(DcSupTaskScore dcSupTaskScore, HttpServletRequest request, HttpServletResponse response) {
		dcSupTaskScore.setPage(new Page<>(request, response));
		if(StringUtils.equals(dcSupTaskScore.getEchart(),"2")){
			String point_office = Global.getConfig("sys.score.pointOffice");
			Office latestUnitByOffice = sysOfficeUnitService.getLatestUnitByOffice();
			if(latestUnitByOffice.getOfficeCode()!=null){
				String code ="";
				String createUnit ="";
				if(StringUtils.isNotBlank(point_office)){
					String[] split = point_office.split(",");
					for(String s :split){
						code =code+"\'"+s+"\',";
					}
					createUnit= code.substring(0,code.length()-1);
					dcSupTaskScore.setCreateUnit(createUnit);
					dcSupTaskScore.setTaskYear( Integer.parseInt(DateUtils.getYear())+"");
					dcSupTaskScore.setLeaderOrgCode(latestUnitByOffice.getOfficeCode());
				}
			}
		}
		Page<Map<String, Object>> page = dcSupTaskScoreService.findPageDes(dcSupTaskScore);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("dceconomicindicator:dcSupTaskScore:view")
	@RequestMapping(value = "form")
	public String form(DcSupTaskScore dcSupTaskScore, Model model) {
		model.addAttribute("dcSupTaskScore", dcSupTaskScore);
		return "modules/task/dceconomicindicator/dcSupTaskScoreForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("dceconomicindicator:dcSupTaskScore:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated DcSupTaskScore dcSupTaskScore) {
		dcSupTaskScoreService.save(dcSupTaskScore);
		return renderResult(Global.TRUE, text("保存任务扣分表成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("dceconomicindicator:dcSupTaskScore:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(DcSupTaskScore dcSupTaskScore) {
		dcSupTaskScoreService.delete(dcSupTaskScore);
		return renderResult(Global.TRUE, text("删除任务扣分表成功！"));
	}
	
}